package com.jk.mindskip.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jk.mindskip.dao.TbStudentMapper;
import com.jk.mindskip.dto.TbStudent;
import com.jk.mindskip.pojo.PagedResponse;
import com.jk.mindskip.service.TbStudentService;
import com.jk.mindskip.utils.PaginationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TbStudentServiceImpl implements TbStudentService {

    @Autowired
    private TbStudentMapper studentMapper;

    @Override
    public TbStudent findTbStudentByUsername(String username) {
        QueryWrapper<TbStudent> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name", username);
        return studentMapper.selectOne(queryWrapper);
    }

    @Override
    public PagedResponse findStudentList(TbStudent tbStudent, Integer pageNum, Integer pageSize) {
        Page<TbStudent> page = new Page<>(pageNum, pageSize);
        QueryWrapper<TbStudent> queryWrapper = new QueryWrapper<>();
        Page<TbStudent> adminPage = studentMapper.selectPage(page, queryWrapper);
        return PaginationUtil.getPaginationResponse(adminPage.getRecords(), pageNum, pageSize, adminPage.getTotal());
    }

    @Override
    public void addStudent(TbStudent tbStudent) {
        studentMapper.insert(tbStudent);
    }

    @Override
    public void deleteById(Integer id) {
        studentMapper.deleteById(id);
    }

    @Override
    public void updateStudent(TbStudent tbStudent) {
        studentMapper.updateById(tbStudent);
    }

    @Override
    public void deleteStudentByIds(List<Integer> ids) {
        studentMapper.deleteBatchIds(ids);
    }
}
